package com.fuershiye.woman.monitor.dao;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fuershiye.woman.monitor.beans.IdNamePid;
import com.fuershiye.woman.monitor.model.DepartmentType;
import com.google.common.collect.Lists;

@Repository("departmentTypeDao")
@SuppressWarnings("unchecked")
public class DepartmentTypeDao {

    @Autowired
    private HibernateDao hibernateDao;

    public void save(DepartmentType it){
        hibernateDao.save(it);
    }

    public void delete(Long id) {
        String sql = "update DepartmentType set status = 0 where id = ?";
        SQLQuery query = hibernateDao.getSqlQuery(sql).addEntity(DepartmentType.class);
        query.setLong(0, id);
        query.executeUpdate();
    }

    public DepartmentType get(Long id) {
    	return hibernateDao.getById(DepartmentType.class,id);
    }

    @SuppressWarnings("unchecked")
    public DepartmentType getTop() {

        String hql = "from DepartmentType ORDER BY id DESC";
        Query query = hibernateDao.getQuery(hql);
        query.setFirstResult(0);
        query.setMaxResults(1);
        List<DepartmentType> result = query.list();
        if(result==null||result.size()<1){
        	return null;
        }
        return result.get(0);
    }

    public DepartmentType findByCode(String code) {
        if(StringUtils.isBlank(code)){
            return null;
        }

        String hql = "from DepartmentType where status = 1 and code = ?";
        Query query = hibernateDao.getQuery(hql);
        query.setString(0, code);
        query.setFirstResult(0);
        query.setMaxResults(1);

        List<DepartmentType> result = query.list();
        if(result==null||result.size()<1){
            return null;
        }
        return result.get(0);
    }

    @SuppressWarnings("unchecked")
	public List<DepartmentType> list(String name) {

        Query query = null;
    	if(StringUtils.isBlank(name)){
            String hql = "from DepartmentType ORDER BY sort DESC";
            query = hibernateDao.getQuery(hql);
    	}else{
            String hql = "from DepartmentType where name like:name ORDER BY sort DESC";
            query = hibernateDao.getQuery(hql);
            query.setString("name","%" + name + "%");
    	}
        return query.list();
    }

    @SuppressWarnings("unchecked")
    public DepartmentType findBySort(Long pos, Integer aim, Integer count) {
		
    	StringBuffer sb = new StringBuffer("from DepartmentType where sort "); 
		if(aim==1){
			sb.append(">:sort ");
		}else{
			sb.append("<:sort ");
		}
		sb.append("order by sort desc");
		
        Query query = hibernateDao.getQuery(sb.toString());
        query.setParameter("sort", pos);
        query.setFirstResult(0);
        query.setMaxResults(count);
        
        List<DepartmentType> result = query.list();
        if(result==null||result.size()<1){
        	return null;
        }
        return result.get(0);
    }

    @SuppressWarnings("unchecked")
    public List<IdNamePid> getAllNames() {

        List<IdNamePid> result = Lists.newArrayList();
        
        String hql = "select id, name from depart_type ORDER BY sort DESC";
        Query query = hibernateDao.getSqlQuery(hql);
        List<Object[]> queryResult = query.list();
        
        for (Object[] fields : queryResult) {
        	IdNamePid idAndName = new IdNamePid();
        	idAndName.setId(fields[0] == null? 0:Long.parseLong(fields[0].toString()));
        	idAndName.setName(fields[1] == null? "":fields[1].toString());
    	  	result.add(idAndName);
		}
	  	return result;
    }    
    
}
